#include "DynamicSequenceTable.h"

// 基本增删查改接口
// 顺序表初始化
void SeqListInit(SeqList* psl, size_t capacity)
{
    psl->m_arr = (SLDataType*)malloc(sizeof(SLDataType) * capacity);
    if(psl->m_arr == NULL)
    {
        printf("初始化失败！\n");
        exit(0);
    }
    memset(psl->m_arr, 0, sizeof(SLDataType)*capacity);
    psl->m_size = 0;
    psl->m_capacity = capacity;
    printf("初始化成功！\n");
}
 // 检查空间，如果满了，进行增容
void CheckCapacity(SeqList* psl)
{
    if(psl->m_size == psl->m_capacity)
    {
        psl->m_arr = (SLDataType*)realloc(psl->m_arr, sizeof(SLDataType)*psl->m_capacity*2);
        if(psl->m_arr == NULL)
        {
            printf("扩容失败！");
            exit(1);
        }
        memset(psl->m_arr+psl->m_capacity, 0, psl->m_capacity);
        psl->m_capacity *= 2;
        printf("扩容成功！");
    }
    else
    {
        printf("空间未满，无需扩容！");
    }
    printf("当前容量为%d\n", psl->m_capacity);
}
 // 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x)
{
    if(psl == NULL)
    {
        printf("空指针！\n");
        exit(3);
    }
    CheckCapacity(psl);
    psl->m_arr[psl->m_size] = x;
    psl->m_size++;
    printf("顺序表尾插成功，插入数据%d\n", x);
}
 // 顺序表尾删
void SeqListPopBack(SeqList* psl)
{
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }
    if(psl->m_size == 0)
    {
        printf("尾删失败，数组为空！\n");
        return;
    }
    psl->m_size--;
    printf("尾删成功！\n");
}
 // 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x)
{
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }

    CheckCapacity(psl);

    for(int i = psl->m_size; i > 0; i--)
    {
        psl->m_arr[i] = psl->m_arr[i-1];
    }
    psl->m_arr[0] = x;
    psl->m_size++;
    printf("头插成功，插入数据为%d\n", x);
}
 // 顺序表头删
void SeqListPopFront(SeqList* psl)
{
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }
    for(int i = 0; i < psl->m_size - 1; i++)
    {
        psl->m_arr[i] = psl->m_arr[i+1];
    }
    psl->m_size--;
    printf("头删成功！\n");
}
 // 顺序表查找
void SeqListFind(SeqList* psl, SLDataType x)
{
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }
    if(psl->m_size == 0)
    {
        printf("顺序表为空！\n");
        return;
    }
    for(int i = 0; i < psl->m_size; i++)
    {
        if(psl->m_arr[i] == x)
        {
            printf("找到了，下标为%d！\n", i);
            return;
        }
    }
    printf("没找到！\n");
}
// 顺序表在pos位置插入x -- pos指位序，为下标+1
 void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
 {
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }

    if(pos < 1 || pos > psl->m_size + 1)
    {
        printf("插入位置不合法！\n");
        return;
    }

    for(int i = psl->m_size; i > pos - 1; i--)
    {
        psl->m_arr[i] = psl->m_arr[i-1];
    }
    psl->m_arr[pos - 1] = x;
    psl->m_size++;
    printf("插入成功！\n");
 }
 // 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos)
{
    if(psl == NULL || psl->m_arr == NULL)
    {
        printf("空指针！\n");
        return;
    }

    if(pos < 1 || pos > psl->m_size)
    {
        printf("删除位置不合法！\n");
        return;
    }

    for(int i = pos-1; i < psl->m_size - 1; i++)
    {
        psl->m_arr[i] = psl->m_arr[i+1];
    }
    psl->m_size--;
    printf("删除成功！\n");
}
 // 顺序表销毁
void SeqListDestory(SeqList* psl)
{
    free(psl->m_arr);
    psl->m_arr = NULL;
    psl->m_size = 0;
    psl->m_capacity = 0;
    printf("销毁成功！\n");
}
 // 顺序表打印
void SeqListPrint(SeqList* psl)
{
    if(psl == NULL)
    {
        printf("空指针！\n");
        return;
    }
    if(psl->m_size == 0)
    {
        printf("顺序表为空！\n");
        return;
    }
    for(int i = 0; i < psl->m_size; i++)
    {
        printf("%d ", psl->m_arr[i]);
    }
    printf("\n");
}